package com.itheima.controller;


import com.itheima.pojo.Emp;
import com.itheima.pojo.Result;
import com.itheima.service.EmpService;
import com.itheima.tials.JwtUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@RestController
public class LoginController {

    @Autowired
    private EmpService empService;

    @PostMapping("/login")
    public Result login(@RequestBody Emp emp){
        Emp e = empService.login(emp);
        if (e != null){
            Map<String,Object> map = new HashMap<>();
            map.put("id",e.getId());
            map.put("username",e.getUsername());
            //生成令牌
            String token = JwtUtils.generateJwt(map);
            return Result.success(token);
        }else {
            return Result.error("登录失败");
        }
    }
}
